using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace MUSA
{
    static class ProjectileManager
    {
        #region Memeber Variables

        //list of projectiles
        static private List<Projectile> projectileList = new List<Projectile>();

        //max life span of porjectile
        static float lifeSpanLimit = 5;

        #endregion

        #region Methods

        /// <summary>
        /// Load new active Projectile into Manager
        /// </summary>
        /// <param name="_p">active Projectile</param>
        static public void addProjectile(Projectile _p)
        {
            projectileList.Add(_p);
        }

        /// <summary>
        /// Checks for Projectile hits upon tank
        /// </summary>
        /// <param name="_t">Tank to check collisions against</param>
        static public void checkCollision(Tank _t)
        {
            List<Projectile> rpl = new List<Projectile>();

            foreach (Projectile p in projectileList)    //cycle throught active projectiles
            {
                if (p.projectileID == _t.TankID)
                {
                    continue;
                }

                //check for impact
                if (_t.Collide( p.getBounding() ))
                {
                    _t.hit(p);                      //notify tank of impact
                    rpl.Add(p);
                }
            }

            foreach( Projectile p in rpl )
            {
                projectileList.Remove(p);
            }
        }

        /// <summary>
        /// Update
        /// </summary>
        /// <param name="gtime">time</param>
        static public void Update(GameTime gtime)
        {
            Stack<int> rind = new Stack<int>();

            int i = 0;
            foreach (Projectile p in projectileList)
            {
                if (p.lifeSpan > lifeSpanLimit)     //check if Projectile has surpassed life span
                {
                    //mark Projectile index for deletion
                    rind.Push(i);
                }
                else
                {
                    //update Projectile
                    p.Update(gtime);
                }

                i++;
            }

            //remove Projectiles marked for deletion
            while (rind.Count != 0)
            {
                projectileList.RemoveAt( rind.Pop() );
            }
        }

        /// <summary>
        /// Draw Projectiles
        /// </summary>
        /// <param name="gtime"></param>
        static public void Draw(GameTime gtime)
        {
            foreach(Projectile p in projectileList)     //cycle through projectiles
            {
                p.Draw(gtime);
            }
        }
        #endregion
    }
}
